package me.ezitku.base.service

import android.app.Service
import android.content.Intent
import android.os.IBinder
import com.ug_project.views.baseShowToast
import me.ezitku.base.extensions.BroadCastReceiver.createBreceiver
import me.ezitku.base.extensions.phone.phoneCallListener
import me.ezitku.base.utils.AudioRecoderUtils

/**
 * تېلفۇن سۆزلەشكەننى ئۈنگە ئېلىش مۇلازىمىتى
 */
class CallRecordService : Service() {
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    private val PHONE_STATE = "android.intent.action.PHONE_STATE"
    //تېلفۇن ئۇرغاندا
    private val OUT_CALL = Intent.ACTION_NEW_OUTGOING_CALL
    private lateinit var recoderUtil: AudioRecoderUtils
    override fun onCreate() {
        super.onCreate()
        recoderUtil = AudioRecoderUtils()
        createBreceiver(arrayOf(PHONE_STATE, OUT_CALL)) {
            var action = it.action
            var outNum = it.getStringExtra(Intent.EXTRA_PHONE_NUMBER)
            if (action.equals(OUT_CALL)) {
                baseShowToast("OutCall: ${outNum}")
            }

        }

        phoneCallListener {
            تېلفۇن_كەلدى { baseShowToast("تېلفۇن كەلدى") }
            تېلفۇن_سۆزلىشىش_باشلاندى { baseShowToast("تېلفۇن سۆزلىشىش باشلاندى!");recoderUtil.startRecord() }
            تېلفۇن_سۆزلىشىش_تامام { baseShowToast("تېلفۇن سۆزلىشىش تامام!");recoderUtil.stopRecord() }
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return START_STICKY
    }
}